package com.utility.project.common.config.mybatisplus;

import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 
 * @author zouke
 * @date 2023/1/29 16:17
 * @description 自定义id生成器.类似雪花算法,长度16. mybatisplus默认长度19,超出了js的范围.  js支持的整数最大2^53-1,这里范围和js保持了一致.
 */
@Component
public class CustomerIdGenerator implements IdentifierGenerator {

    private final Integer MACHINE_ID = 1;

    @PostConstruct
    private void initMachineId() {
        IdGenerator.initDefaultInstance(MACHINE_ID);
    }

    @Override
    public Long nextId(Object entity) {
        // 自定义ID生成器.
        return IdGenerator.generateId();
    }
}
